<?php 
	include '../init.php';
	$a=$_GET['a'];
	$model=new Model('advertisement');

	switch ($a) 
	{
		case 'add':
			$data['name']=Upload('pic',PATH.'/public/uploads/');
			$data['location']=$_POST['location'];
			//插入数据库
			$res=$model->insert($data);
			if ($res) 
			{
				success('添加成功！',ADMIN_URL.'/advertisement/index.php',1);

				return false;
			}else{
				error('广告添加失败！');
				return false;
			}
			break;
		
		case 'show':
			//接受当前页
			$p=$_GET['p'];

			$id=$_GET['id'];
			//先查询 同一个广告位在前台只显示一张图
			$sql="select `location` from `advertisement` where id=$id";
			$location=$model->find($sql)[0]['location'];
			$sql1="update `advertisement` set status=0 where location=$location";
			$res=$model->find($sql1);

			$sql2="update `advertisement` set status=1 where id=$id";
			$res1=$model->find($sql2);

			if (empty($res1)) 
			{
				header('location:'.ADMIN_URL.'/advertisement/index.php?p='.$p);
				return false;
			}
			break;

		case 'del':
			$id=$_GET['id'];
			//查询图片名称
			$sql="select name from `advertisement` where id=$id";
			$list=$model->find($sql)[0]['name'];
			//var_dump($list);
			//删除
			$res=$model->delete($id);
			if ($res) 
			{
				//删除图片路径
				unlink(PATH.'/public/uploads/'.substr($list, 0,4).'/'.substr($list, 4,2).'/'.substr($list, 6,2).'/'.$list);
				unlink(PATH.'/public/uploads/'.substr($list, 0,4).'/'.substr($list, 4,2).'/'.substr($list, 6,2).'/50_'.$list);
				unlink(PATH.'/public/uploads/'.substr($list, 0,4).'/'.substr($list, 4,2).'/'.substr($list, 6,2).'/80_'.$list);
				unlink(PATH.'/public/uploads/'.substr($list, 0,4).'/'.substr($list, 4,2).'/'.substr($list, 6,2).'/200_'.$list);
				unlink(PATH.'/public/uploads/'.substr($list, 0,4).'/'.substr($list, 4,2).'/'.substr($list, 6,2).'/500_'.$list);

				success('删除成功',ADMIN_URL.'/advertisement/index.php',1);
				return false;
			}
			break;
	}
?>